﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using SQLite.CodeFirst;

namespace WeManageService
{
	public class Plan
	{
		[Key, Column(Order = 1)]
		public int PlanID { get; set; }             //自动识别为主键
		public string Name { get; set; }            //plan名---Title
		public string Description { get; set; }     //描述
		public string Address { get; set; }         //地点
		public DateTime StartTime { get; set; }     //开始时间
		public DateTime NotifyTime { get; set; }    //提醒时间
		public DateTime EndTime { get; set; }       //结束时间
		public int LoopPeriod { get; set; }          //循环天数 0 表示不循环
		public bool Delayable { get; set; }
		public bool Deletable { get; set; }
		public int PState { get; set; }             //0为未完成，1为已完成，2为已超时
		public int Score { get; set; }
		public string Comment { get; set; }

		[NotMapped]
		public string PStateInText
		{
			get
			{
				switch (PState)
				{
					case 0: return "未完成";
					case 1: return "已完成";
					case 2: return "已超时";
				}
				return "";
			}
		}


		[NotMapped]
		public string LoopPeriodInText
		{
			get
			{
				if (LoopPeriod == 0) return "单次";
				else if (LoopPeriod % 7 == 0) return "每 " + (LoopPeriod / 7).ToString() + " 周";
				else return "每 " + LoopPeriod.ToString() + " 天";
			}
		}

		[NotMapped]
		public string WeekFormDisplay
		{
			get => Name + "   " + EndTime.ToShortTimeString();
		}

		public string UserName { get; set; }

		public Plan() { }
		public Plan(Plan plan)
		{
			PlanID = plan.PlanID;
			Name = plan.Name;
			Description = plan.Description;
			Address = plan.Address;
			StartTime = plan.StartTime;
			NotifyTime = plan.NotifyTime;
			EndTime = plan.EndTime;
			LoopPeriod = plan.LoopPeriod;
			Delayable = plan.Delayable;
			Deletable = plan.Deletable;
			PState = plan.PState;
			Score = plan.Score;
			Comment = plan.Comment;
			UserName = plan.UserName;
		}
		public Plan(int id, string name, string des, string add, DateTime st, DateTime nt, DateTime et,
			int lp, bool del, bool dlt, int pstate, int score, string comment, string username)
		{
			PlanID = id;
			Name = name;
			Description = des;
			Address = add;
			StartTime = st;
			NotifyTime = nt;
			EndTime = et;
			LoopPeriod = lp;
			Delayable = del;
			Deletable = dlt;
			PState = pstate;
			Score = score;
			Comment = comment;
			UserName = username;
		}
		public Plan(int id, string name, string des, string add)
		{
			PlanID = id;
			Name = name;
			Description = des;
			Address = add;
		}
		
		public Plan NextLoop()
		{
			Plan plan = new Plan(this);
			plan.StartTime = StartTime.AddDays(LoopPeriod);
			plan.EndTime = EndTime.AddDays(LoopPeriod);
			plan.NotifyTime = NotifyTime.AddDays(LoopPeriod);
			plan.PState = 0;
			return plan;
		}
	}
}
